<style>
.productInfo .el-input.is-disabled .el-input__inner{
  color:black;
}
.productInfo .el-textarea.is-disabled .el-textarea__inner{
  color:black;
}
.productInfo .el-upload--picture-card{
  overflow: hidden;
}
.productInfo img{
  width: 100%;
  height: 100%;
}
.video-avatar{
  width:350px;
  height:250px;
}
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入产品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择产品类型" clearable size="small">
          <el-option
          v-for="dict in productTypeOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="examineResult">
        <el-select v-model="queryParams.examineResult" placeholder="请选择审核状态" clearable size="small">
          <el-option
          v-for="dict in exaResuOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:info:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:info:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" :border="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="产品编号" align="center" prop="no" />
      <el-table-column label="产品名称" align="center" prop="name" />
      <el-table-column label="产品类型" align="center" prop="type" :formatter="typeFmt" />
      <el-table-column label="产品价格" align="center" prop="price" />
      <el-table-column label="产品面额" align="center" prop="facePrice" />
      <el-table-column label="使用规则描述" align="center" prop="useRuleDesc" />
      <el-table-column label="产品开始日期" align="center" prop="expireStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ dateFmt(scope.row.expireStartDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品结束日期" align="center" prop="expireEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ dateFmt(scope.row.expireEndDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:info:edit']"
          >修改</el-button>
          <el-button
            v-if="scope.row.examineResult == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleAudit(scope.row)"
            v-hasPermi="['product:info:audit']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['product:info:query']"
          >详细</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改产品信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择产品类型">
            <el-option
            v-for="dict in productTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品描述" prop="description">
          <el-input v-model="form.description" placeholder="请输入产品描述" />
        </el-form-item>
        <el-form-item label="产品价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入产品价格" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')" />
        </el-form-item>
        <el-form-item label="产品面额" prop="facePrice">
          <el-input v-model="form.facePrice" placeholder="请输入产品面额" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')" />
        </el-form-item>
        <el-form-item label="产品开始日期" prop="expireStartDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.expireStartDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择产品开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产品结束日期" prop="expireEndDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.expireEndDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择产品结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用规则描述" prop="useRuleDesc">
          <el-input v-model="form.useRuleDesc" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="展示小图" prop="smallImg">
          <uploadImage v-model="form.smallImg"/>
        </el-form-item>
        <el-form-item label="展示大图" prop="bigImg">
          <uploadImage v-model="form.bigImg"/>
        </el-form-item>
        <el-form-item label="展示图片1" prop="showImg1">
          <uploadImage v-model="form.showImg1"/>
        </el-form-item>
        <el-form-item label="展示图片2" prop="showImg2">
          <uploadImage v-model="form.showImg2"/>
        </el-form-item>
        <el-form-item label="展示视频" prop="showVideo" ref="showVideo">
          <el-upload
            accept=".mp4"
            :action="uploadImgUrl"
            :on-success="handleUploadSuccess"
            :on-change="handleChange"
            :before-upload="handleBeforeUpload"
            :on-error="handleUploadError"
            :show-file-list="false">
            <video v-if="form.showVideo"
              :src="form.showVideo"
              class="avatar video-avatar"
              controls="controls">
              您的浏览器不支持视频播放
            </video>
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button v-if="form.examineResult == 0" type="primary" @click="handleViewAudit">审 核</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="500px" append-to-body class="productInfo">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="产品编号" prop="no">
          <el-input disabled v-model="form.no" placeholder="请输入产品编号" />
        </el-form-item>
        <el-form-item label="产品名称" prop="name">
          <el-input disabled v-model="form.name" placeholder="请输入产品名称" />
        </el-form-item>
        <el-form-item label="产品类型" prop="type">
          <el-select disabled v-model="form.type" placeholder="请选择产品类型">
            <el-option
            v-for="dict in productTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="产品描述" prop="description">
          <el-input disabled v-model="form.description" placeholder="请输入产品描述" />
        </el-form-item>
        <el-form-item label="产品价格" prop="price" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')">
          <el-input disabled v-model="form.price" placeholder="请输入产品价格" />
        </el-form-item>
        <el-form-item label="产品面额" prop="facePrice" oninput="value=value.replace(/^(0+)|[^\d]+/g,'')">
          <el-input disabled v-model="form.facePrice" placeholder="请输入产品面额" />
        </el-form-item>
        <el-form-item label="产品开始日期" prop="expireStartDate">
          <el-date-picker disabled clearable size="small" style="width: 200px"
            v-model="form.expireStartDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择产品开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="产品结束日期" prop="expireEndDate">
          <el-date-picker disabled clearable size="small" style="width: 200px"
            v-model="form.expireEndDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择产品结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用规则描述" prop="useRuleDesc">
          <el-input disabled v-model="form.useRuleDesc" type="textarea" placeholder="请输入内容" style="color:black;" />
        </el-form-item>
        <el-form-item label="展示小图">
          <div>
            <div class="el-upload--picture-card">
              <div>
                <img :src="form.smallImg" alt="">
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="展示大图">
          <div>
            <div class="el-upload--picture-card">
              <div>
                <img :src="form.bigImg" alt="">
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="展示图片1">
          <div>
            <div class="el-upload--picture-card">
              <div>
                <img :src="form.showImg1" alt="">
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="展示图片2">
          <div>
            <div class="el-upload--picture-card">
              <div>
                <img :src="form.showImg2" alt="">
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="展示视频">
          <video v-if="form.showVideo"
            :src="form.showVideo"
            class="avatar video-avatar"
            controls="controls">
            您的浏览器不支持视频播放
          </video>
        </el-form-item>
        <el-form-item label="创建人" prop="buildOperator">
          <el-input disabled v-model="form.buildOperator" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建日期" prop="buildDate">
          <el-date-picker disabled clearable size="small" style="width: 200px"
            v-model="form.buildDate"
            type="date"
            value-format="yyyyMMdd"
            placeholder="选择产品结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确 定</el-button>
        <el-button v-if="form.examineResult == 0" type="primary" @click="handleViewAudit">审 核</el-button>
      </div>
    </el-dialog>

    <!-- 审核对话框 -->
    <el-dialog :title="title" :visible.sync="auditOpen" width="500px" append-to-body>
      <el-form ref="auditForm" :model="auditForm" :rules="auditRules" label-width="80px">
        <el-form-item label="审核结果" prop="examineResult">
          <el-select v-model="auditForm.examineResult" >
            <el-option
            v-for="dict in exaResuOptionsAddAndUpdate"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见" prop="examineConclusion">
          <el-input v-model="auditForm.examineConclusion" />
        </el-form-item>
        <el-form-item label="审核截止日期" prop="examineEndDate">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="auditForm.examineEndDate"
            type="date"
            value-format="yyyyMMdd"
            >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="auditFormSubmit" v-hasPermi="['merchant:template:audit']">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo, listInfoVo, getInfoVo, auditInfo } from "@/api/product/info";
import UploadImage from '@/components/UploadImage';
import { getToken } from "@/utils/auth";

export default {
  name: "Info",
  components: {
    UploadImage,
  },
  data() {
    return {
      // 上传的图片服务器地址
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/uploadToLongFileSystem",
      headers: {
        toekn: getToken(),
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      nos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品信息表格数据
      infoList: [],
      // 产品类型字典
      productTypeOptions: [],
      // 审核结果字典
      exaResuOptions: [],
      // 审核结果字典
      exaResuOptionsAddAndUpdate: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openView: false,
      // 是否显示弹出层
      auditOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        name: null,
        type: null,
        description: null,
        price: null,
        facePrice: null,
        expireStartDate: null,
        expireEndDate: null,
        useRuleDesc: null,
        smallImg: null,
        bigImg: null,
        showImg1: null,
        showImg2: null,
        showVideo: null,
        examineCode: null,
        buildOperator: null,
        buildDate: null,
        buildTime: null,
        modifiedOperator: null,
        modifiedDate: null,
        modifiedTime: null,
        examineResult: null
      },
      // 表单参数
      form: {},
      // 审核表单参数
      auditForm: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "产品名称不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "产品类型不能为空", trigger: "blur" }
        ],
        description: [
          { required: true, message: "产品描述不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "产品价格不能为空", trigger: "blur" }
        ],
        facePrice: [
          { required: true, message: "产品面额不能为空", trigger: "blur" }
        ],
        expireStartDate: [
          { required: true, message: "产品开始日期不能为空", trigger: "blur" }
        ],
        expireEndDate: [
          { required: true, message: "产品结束日期不能为空", trigger: "blur" }
        ],
        useRuleDesc: [
          { required: true, message: "使用规则描述不能为空", trigger: "blur" }
        ],
        smallImg: [
          { required: true, message: "展示小图不能为空", trigger: "blur" }
        ],
        bigImg: [
          { required: true, message: "展示大图不能为空", trigger: "blur" }
        ],
        showImg1: [
          { required: true, message: "展示图片1不能为空", trigger: "blur" }
        ],
      },
      //审核信息表单校验
      auditRules: {
        examineResult: [
          { required: true, message: "审核结果不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("pdt_product_type").then(response => {
      this.productTypeOptions = response.data;
    });
    this.getDicts("msg_message_template_examineResult_type").then(response => {
      this.exaResuOptions = response.data;
      this.exaResuOptions.forEach(element => {
        if(element.dictValue !=0){
          this.exaResuOptionsAddAndUpdate.push(element)
        }
      });
    });
  },
  methods: {
    /** 查询产品信息列表 */
    getList() {
      this.loading = true;
      listInfoVo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openView = false;
      this.auditOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        name: null,
        type: null,
        description: null,
        price: null,
        facePrice: null,
        expireStartDate: null,
        expireEndDate: null,
        useRuleDesc: null,
        smallImg: null,
        bigImg: null,
        showImg1: null,
        showImg2: null,
        showVideo: null,
        examineCode: null,
        buildOperator: null,
        buildDate: null,
        buildTime: null,
        modifiedOperator: null,
        modifiedDate: null,
        modifiedTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.nos = selection.map(item => item.no)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getInfoVo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品信息";
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id || this.ids
      getInfoVo(id).then(response => {
        this.form = response.data;
        this.openView = true;
        this.title = "产品详细信息";
      });
    },
    /** 审核按钮 */
    handleAudit(row) {
      const nos = row? row.no? [row.no] : this.nos : this.nos;
      this.auditForm.infoNos = nos;
      this.auditOpen = true;
    },
    /** 详情审核按钮 */
    handleViewAudit() {
      this.auditForm.infoNos = [this.form.no];
      this.auditOpen = true;
    },
    /** 审核提交按钮 */
    auditFormSubmit(row) {
      this.$refs.auditForm.validate(valid => {
        if(valid){
          auditInfo(this.auditForm).then(response => {
            this.msgSuccess("审核成功");
            this.auditOpen = false;
            this.getList();
          });
        }
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除产品信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInfo(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有产品信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfo(queryParams);
        }).catch(() => {});
    },
    //文件上传成功
    handleUploadSuccess(res) {
      this.form.showVideo = res.url;
      this.loading.close();
      this.$refs.showVideo.clearValidate();
    },
    //文件上传之前
    handleBeforeUpload() {
      this.loading = this.$loading({
        lock: true,
        text: "上传中",
        background: "rgba(0, 0, 0, 0.7)",
      });
    },
    //文件上传失败
    handleUploadError() {
      this.$message({
        type: "error",
        message: "上传失败",
      });
      this.loading.close();
    },
    //文件选择框被修改
    handleChange(file, fileList) {
      // 当多余一个的时候替换文件
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }
    },
    /** 产品类型格式化 */
    typeFmt(row, column) {
      var val = row.type.trim();
      this.productTypeOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 日期格式化 */
    dateFmt(str) {
      return str.substring(0,4)+'-'+str.substring(4,6)+'-'+str.substring(6,8);
    }
  }
};
</script>
